3 * Implements Special:Import
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup SpecialPage
28 * MediaWiki page data importer
30 * @ingroup SpecialPage
32 class SpecialImport
extends SpecialPage
{
33 private $sourceName = false;
34 private $interwiki = false;
36 private $fullInterwikiPrefix;
37 private $mapping = 'default';
39 private $rootpage = '';
40 private $frompage = '';
41 private $logcomment = false;
42 private $history = true;
43 private $includeTemplates = false;
44 private $pageLinkDepth;
49 public function __construct() {
50 parent
::__construct( 'Import', 'import' );
55 * @param string|null $par
56 * @throws PermissionsError
57 * @throws ReadOnlyError
59 function execute( $par ) {
60 $this->useTransactionalTimeLimit();
63 $this->outputHeader();
65 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
67 $this->getOutput()->addModules( 'mediawiki.special.import' );
69 $user = $this->getUser();
70 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
71 throw new PermissionsError( 'import' );
74 # @todo Allow Title::getUserPermissionsErrors() to take an array
75 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
76 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
77 $errors = wfMergeErrorArrays(
78 $this->getPageTitle()->getUserPermissionsErrors(
79 'import', $user, true,
80 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
82 $this->getPageTitle()->getUserPermissionsErrors(
83 'importupload', $user, true,
84 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
89 throw new PermissionsError( 'import', $errors );
92 $this->checkReadOnly();
94 $request = $this->getRequest();
95 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
102 * Do the actual import
104 private function doImport() {
106 $request = $this->getRequest();
107 $this->sourceName
= $request->getVal( "source" );
109 $this->logcomment
= $request->getText( 'log-comment' );
110 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
112 : $request->getIntOrNull( 'pagelink-depth' );
114 $this->mapping
= $request->getVal( 'mapping' );
115 if ( $this->mapping
=== 'namespace' ) {
116 $this->namespace = $request->getIntOrNull( 'namespace' );
117 } elseif ( $this->mapping
=== 'subpage' ) {
118 $this->rootpage
= $request->getText( 'rootpage' );
120 $this->mapping
= 'default';
123 $user = $this->getUser();
124 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
125 $source = Status
::newFatal( 'import-token-mismatch' );
126 } elseif ( $this->sourceName
=== 'upload' ) {
128 if ( $user->isAllowed( 'importupload' ) ) {
129 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
131 throw new PermissionsError( 'importupload' );
133 } elseif ( $this->sourceName
=== 'interwiki' ) {
134 if ( !$user->isAllowed( 'import' ) ) {
135 throw new PermissionsError( 'import' );
137 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
138 // does this interwiki have subprojects?
139 $importSources = $this->getConfig()->get( 'ImportSources' );
140 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
141 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
142 $source = Status
::newFatal( "import-invalid-interwiki" );
144 if ( $hasSubprojects ) {
145 $this->subproject
= $request->getVal( 'subproject' );
146 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
148 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
149 $source = Status
::newFatal( "import-invalid-interwiki" );
151 $this->history
= $request->getCheck( 'interwikiHistory' );
152 $this->frompage
= $request->getText( "frompage" );
153 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
154 $source = ImportStreamSource
::newFromInterwiki(
155 $this->fullInterwikiPrefix
,
158 $this->includeTemplates
,
159 $this->pageLinkDepth
);
163 $source = Status
::newFatal( "importunknownsource" );
166 $out = $this->getOutput();
167 if ( !$source->isGood() ) {
169 "<p class=\"error\">\n$1\n</p>",
170 array( 'importfailed', $source->getWikiText() )
173 $importer = new WikiImporter( $source->value
, $this->getConfig() );
174 if ( !is_null( $this->namespace ) ) {
175 $importer->setTargetNamespace( $this->namespace );
176 } elseif ( !is_null( $this->rootpage
) ) {
177 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
178 if ( !$statusRootPage->isGood() ) {
180 "<p class=\"error\">\n$1\n</p>",
182 'import-options-wrong',
183 $statusRootPage->getWikiText(),
184 count( $statusRootPage->getErrorsArray() )
192 $out->addWikiMsg( "importstart" );
194 $reporter = new ImportReporter(
197 $this->fullInterwikiPrefix
,
200 $reporter->setContext( $this->getContext() );
205 $importer->doImport();
206 } catch ( Exception
$e ) {
209 $result = $reporter->close();
212 # No source or XML parse error
214 "<p class=\"error\">\n$1\n</p>",
215 array( 'importfailed', $exception->getMessage() )
217 } elseif ( !$result->isGood() ) {
220 "<p class=\"error\">\n$1\n</p>",
221 array( 'importfailed', $result->getWikiText() )
225 $out->addWikiMsg( 'importsuccess' );
227 $out->addHTML( '<hr />' );
231 private function getMappingFormPart( $sourceName ) {
232 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
233 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
237 <td class='mw-input'>" .
239 $this->msg( 'import-mapping-default' )->text(),
242 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
243 "mw-import-mapping-$sourceName-default",
244 ( $isSameSourceAsBefore ?
245 ( $this->mapping
=== 'default' ) :
246 is_null( $defaultNamespace ) )
253 <td class='mw-input'>" .
255 $this->msg( 'import-mapping-namespace' )->text(),
258 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
259 "mw-import-mapping-$sourceName-namespace",
260 ( $isSameSourceAsBefore ?
261 ( $this->mapping
=== 'namespace' ) :
262 !is_null( $defaultNamespace ) )
264 Html
::namespaceSelector(
266 'selected' => ( $isSameSourceAsBefore ?
268 ( $defaultNamespace ||
'' ) ),
270 'name' => "namespace",
271 // mw-import-namespace-interwiki, mw-import-namespace-upload
272 'id' => "mw-import-namespace-$sourceName",
273 'class' => 'namespaceselector',
281 <td class='mw-input'>" .
283 $this->msg( 'import-mapping-subpage' )->text(),
286 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
287 "mw-import-mapping-$sourceName-subpage",
288 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
290 Xml
::input( 'rootpage', 50,
291 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
293 // Should be "mw-import-rootpage-...", but we keep this inaccurate
294 // ID for legacy reasons
295 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
296 'id' => "mw-interwiki-rootpage-$sourceName",
304 private function showForm() {
305 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
306 $user = $this->getUser();
307 $out = $this->getOutput();
308 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
309 $importSources = $this->getConfig()->get( 'ImportSources' );
311 if ( $user->isAllowed( 'importupload' ) ) {
312 $mappingSelection = $this->getMappingFormPart( 'upload' );
314 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
318 'enctype' => 'multipart/form-data',
321 'id' => 'mw-import-upload-form'
324 $this->msg( 'importtext' )->parseAsBlock() .
325 Html
::hidden( 'action', 'submit' ) .
326 Html
::hidden( 'source', 'upload' ) .
327 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
329 <td class='mw-label'>" .
330 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
332 <td class='mw-input'>" .
333 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
337 <td class='mw-label'>" .
338 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
340 <td class='mw-input'>" .
341 Xml
::input( 'log-comment', 50,
342 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
343 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
349 <td class='mw-submit'>" .
350 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
353 Xml
::closeElement( 'table' ) .
354 Html
::hidden( 'editToken', $user->getEditToken() ) .
355 Xml
::closeElement( 'form' ) .
356 Xml
::closeElement( 'fieldset' )
359 if ( empty( $importSources ) ) {
360 $out->addWikiMsg( 'importnosources' );
364 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
365 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
367 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
369 <td class='mw-label'>" .
370 $this->msg( 'export-pagelinks' )->parse() .
372 <td class='mw-input'>" .
373 Xml
::input( 'pagelink-depth', 3, 0 ) .
377 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
380 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
386 'id' => 'mw-import-interwiki-form'
389 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
390 Html
::hidden( 'action', 'submit' ) .
391 Html
::hidden( 'source', 'interwiki' ) .
392 Html
::hidden( 'editToken', $user->getEditToken() ) .
393 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
395 <td class='mw-label'>" .
396 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
398 <td class='mw-input'>" .
401 array( 'name' => 'interwiki', 'id' => 'interwiki' )
405 $needSubprojectField = false;
406 foreach ( $importSources as $key => $value ) {
407 if ( is_int( $key ) ) {
409 } elseif ( $value !== $key ) {
410 $needSubprojectField = true;
416 if ( is_array( $value ) ) {
417 $attribs['data-subprojects'] = implode( ' ', $value );
419 if ( $this->interwiki
=== $key ) {
420 $attribs['selected'] = 'selected';
422 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
426 Xml
::closeElement( 'select' )
429 if ( $needSubprojectField ) {
433 array( 'name' => 'subproject', 'id' => 'subproject' )
437 $subprojectsToAdd = array();
438 foreach ( $importSources as $key => $value ) {
439 if ( is_array( $value ) ) {
440 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
443 $subprojectsToAdd = array_unique( $subprojectsToAdd );
444 sort( $subprojectsToAdd );
445 foreach ( $subprojectsToAdd as $subproject ) {
446 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
450 Xml
::closeElement( 'select' )
458 <td class='mw-label'>" .
459 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
461 <td class='mw-input'>" .
462 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
468 <td class='mw-input'>" .
470 $this->msg( 'import-interwiki-history' )->text(),
480 <td class='mw-input'>" .
482 $this->msg( 'import-interwiki-templates' )->text(),
483 'interwikiTemplates',
484 'interwikiTemplates',
485 $this->includeTemplates
491 <td class='mw-label'>" .
492 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
494 <td class='mw-input'>" .
495 Xml
::input( 'log-comment', 50,
496 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
497 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
504 <td class='mw-submit'>" .
506 $this->msg( 'import-interwiki-submit' )->text(),
507 Linker
::tooltipAndAccesskeyAttribs( 'import' )
511 Xml
::closeElement( 'table' ) .
512 Xml
::closeElement( 'form' ) .
513 Xml
::closeElement( 'fieldset' )
518 protected function getGroupName() {
525 * @ingroup SpecialPage
527 class ImportReporter
extends ContextSource
{
528 private $reason = false;
529 private $mOriginalLogCallback = null;
530 private $mOriginalPageOutCallback = null;
531 private $mLogItemCount = 0;
534 * @param WikiImporter $importer
535 * @param bool $upload
536 * @param string $interwiki
537 * @param string|bool $reason
539 function __construct( $importer, $upload, $interwiki, $reason = false ) {
540 $this->mOriginalPageOutCallback
=
541 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
542 $this->mOriginalLogCallback
=
543 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
544 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
545 $this->mPageCount
= 0;
546 $this->mIsUpload
= $upload;
547 $this->mInterwiki
= $interwiki;
548 $this->reason
= $reason;
552 $this->getOutput()->addHTML( "<ul>\n" );
555 function reportNotice( $msg, array $params ) {
556 $this->getOutput()->addHTML(
557 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
561 function reportLogItem( /* ... */ ) {
562 $this->mLogItemCount++
;
563 if ( is_callable( $this->mOriginalLogCallback
) ) {
564 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
569 * @param Title $title
570 * @param ForeignTitle $foreignTitle
571 * @param int $revisionCount
572 * @param int $successCount
573 * @param array $pageInfo
576 function reportPage( $title, $foreignTitle, $revisionCount,
577 $successCount, $pageInfo ) {
578 $args = func_get_args();
579 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
581 if ( $title === null ) {
582 # Invalid or non-importable title; a notice is already displayed
588 if ( $successCount > 0 ) {
589 $this->getOutput()->addHTML(
590 "<li>" . Linker
::linkKnown( $title ) . " " .
591 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
595 if ( $this->mIsUpload
) {
596 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
597 $successCount )->inContentLanguage()->text();
598 if ( $this->reason
) {
599 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
604 $interwiki = '[[:' . $this->mInterwiki
. ':' .
605 $foreignTitle->getFullText() . ']]';
606 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
607 $successCount )->params( $interwiki )->inContentLanguage()->text();
608 if ( $this->reason
) {
609 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
612 $action = 'interwiki';
615 $logEntry = new ManualLogEntry( 'import', $action );
616 $logEntry->setTarget( $title );
617 $logEntry->setComment( $detail );
618 $logEntry->setPerformer( $this->getUser() );
619 $logid = $logEntry->insert();
620 $logEntry->publish( $logid );
622 $comment = $detail; // quick
623 $dbw = wfGetDB( DB_MASTER
);
624 $latest = $title->getLatestRevID();
625 $nullRevision = Revision
::newNullRevision(
627 $title->getArticleID(),
633 if ( !is_null( $nullRevision ) ) {
634 $nullRevision->insertOn( $dbw );
635 $page = WikiPage
::factory( $title );
637 $page->updateRevisionOn( $dbw, $nullRevision );
639 'NewRevisionFromEditComplete',
640 array( $page, $nullRevision, $latest, $this->getUser() )
644 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
645 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
650 $out = $this->getOutput();
651 if ( $this->mLogItemCount
> 0 ) {
652 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
653 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
654 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
655 $out->addHTML( "</ul>\n" );
657 return Status
::newFatal( 'importnopages' );
659 $out->addHTML( "</ul>\n" );
661 return Status
::newGood( $this->mPageCount
);